package com.service.impl;

import com.dao.GroupInfoDao;
import com.dao.GroupUserDao;
import com.dao.GroupVerifyMessageDao;
import com.entity.GroupInfo;
import com.entity.GroupUser;
import com.entity.GroupVerifyMessage;
import com.service.GroupInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 群信息业务实现
 *
 * @author lbb
 * @version 1.0
 * @date 2020.3.20
 */
@Service
public class GroupInfoServiceImpl implements GroupInfoService {
	//群信息
	@Autowired
	private GroupInfoDao groupInfoDao;
	//群成员
	@Autowired
	private GroupUserDao groupUserDao;
	@Autowired
	private GroupVerifyMessageDao groupVerifyMessageDao;
	
	//修改
	@Override
	public Boolean editGroupInfo(GroupInfo groupInfo) {
		return groupInfoDao.editGroupInfo(groupInfo);
	}
	
	//根据群号查找
	@Override
	public GroupInfo getByNumGroupInfo(String groupNum) {
		return groupInfoDao.getByNumGroupInfo(groupNum);
	}
	
	//添加群
	@Transactional
	@Override
	public Boolean addGroupInfo(GroupInfo groupInfo, Integer userId) {
		groupInfoDao.addGroupInfo(groupInfo);
		//成员信息
		GroupUser groupUser = new GroupUser();
		groupUser.setUserId(userId);
		groupUser.setGroupId(groupInfo.getId());
		groupUser.setGroupRole(0);
		groupUser.setIsSilent(0);
		groupUser.setNotReadCount(0);
		groupUser.setIsRemove(0);
		
		return groupUserDao.addGroupUser(groupUser);
	}

	//解散群
	@Override
	@Transactional
	public Boolean delGroupInfo(Integer groupId) {
		//删除群用户
		groupUserDao.delByGroupId(groupId);
		//删除群验证消息
		groupVerifyMessageDao.delByGroupId(groupId);
		//删除群
		groupInfoDao.delGroupInfo(groupId);
		return true;
	}


}
